package jrmob.yonyou.com.commonlib;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import jrmob.yonyou.com.commonlib.baseui.base.BaseActivityMob_lib;

/**
 * Created by Marvin on 2016/11/18.
 */

public class AppManager {
    private static AppManager instance;
    private Context context;
    private List<BaseActivityMob_lib> activityList = new LinkedList<BaseActivityMob_lib>();
    private AppManager(Context context) {
        this.context = context.getApplicationContext();
    }

    public static AppManager getInstance(Context context) {
        if(instance == null){
            instance = new AppManager(context);
        }
        return instance;
    }
    // 添加Activity到容器中
    public void addActivity(BaseActivityMob_lib activity) {
        synchronized (activityList) {
            activityList.add(activity);
        }
    }
    /**
     * 退出整个应用
     */
    public void exit() {

        List<BaseActivityMob_lib> copy = null;
        synchronized (activityList) {
            if (null != activityList && activityList.size() > 0) {
                copy = new ArrayList<BaseActivityMob_lib>(activityList);
            }
        }
        try {
            if (null != copy && copy.size() > 0) {
                for (Activity activity : copy) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
            activityList.clear();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }

        // 一定要清空 by zhangg
        if (null != activityList && activityList.size() > 0) {
            activityList.clear();
        }
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
